<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="设备编号">
              <a-input placeholder="请输入设备编号" v-model="queryParam.devSn"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="设备名称">
              <a-input placeholder="请输入设备名称" v-model="queryParam.devName"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备类型">
                <j-dict-select-tag placeholder="请选择设备类型" v-model="queryParam.devType" dictCode="dev_type"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备IP">
                <a-input placeholder="请输入设备IP" v-model="queryParam.devIp"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
                <j-dict-select-tag placeholder="请选择状态" v-model="queryParam.devStatus" dictCode="ol_status"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('设备管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" style="color: #00A0E9;">编辑</a>

          <span v-if="record.devType==3">
            <a-divider type="vertical" />
            <a @click="handleRecordList(record)" style="color:#0abfbf;">抓拍记录</a>
          </span>
          <span v-else>
            <a-divider type="vertical" />
            <a @click="handleRecordList(record)" style="color: #0abfbf;">通行记录</a>
          </span>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?"  @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
          </a-popconfirm>
        </span>
        <span slot="olStatus" slot-scope="text, record">
          <a-tooltip placement="top" trigger="click" v-if="record.heartStatus==1" @click="showDevHeart(record.heartData)">
            <template slot="title">
              <span>
                <a-row style="width: 200px;">
                  <a-col :span="24" style="color: green;background: red;"><a-alert message="在线" type="success" show-icon /></a-col>
                </a-row>
                <span>
                  <a-row>
                    <a-col :span="12">运行时长：</a-col>
                    <a-col style="color: white">{{devHeart.runtime}}</a-col>
                  </a-row>
                  <a-row>
                    <a-col :span="12">重启次数：</a-col>
                    <a-col style="color: white">{{devHeart.rebootcount}}</a-col>
                  </a-row>
                  <a-row>
                    <a-col :span="12">核心温度：</a-col>
                    <a-col style="color: white">{{devHeart.temperature}}&#8451;  </a-col>
                  </a-row>
                  <a-row>
                    <a-col :span="12">计算单元：</a-col>
                    <a-col style="color: white">{{devHeart.xeyeruntime}}</a-col>
                  </a-row>
                 </span>
              </span>
            </template>
             <a-alert message="在线" type="success" show-icon />
          </a-tooltip>
          <a-alert v-if="record.heartStatus==0" message="离线" type="error" show-icon />
        </span>
      </a-table>
    </div>

    <devices-modal ref="modalForm" @ok="modalFormOk"></devices-modal>
    <devices-record-modal ref="recordModal"></devices-record-modal>
    <dev-heart-modal ref="heartModal"></dev-heart-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import DevicesModal from './modules/DevicesModal'
  import DevicesRecordModal from '../devicesrecord/DevicesRecordModal'
  import DevHeartModal from "./modules/DevHeartModal";

  export default {
    name: 'DevicesList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      DevicesModal,
      DevicesRecordModal,
      DevHeartModal
    },
    data () {
      return {
        description: '设备管理管理页面',
        devHeart:{},
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title: '在线状态',
            dataindex: 'heartStatus',
            align:"center",
            width:80,
            scopedSlots: { customRender: 'olStatus' },
          },
          {
            title:'设备编号',
            align:"center",
            dataIndex: 'devSn'
          },
          {
            title:'设备名称',
            align:"center",
            dataIndex: 'devName'
          },
          {
            title:'设备类型',
            align:"center",
            dataIndex: 'devType_dictText'
          },
          {
            title:'设备归属单位',
            align:"center",
            dataIndex: 'devOwner'
          },
          {
            title:'安装地址',
            align:"center",
            dataIndex: 'devAddress'
          },
          {
            title:'详细地址',
            align:"center",
            dataIndex: 'devAddressDetail'
          },
          {
            title:'方向',
            align:"center",
            dataIndex: 'devDirection'
          },
          {
            title:'设备坐标',
            align:"center",
            dataIndex: 'devLocation'
          },
          {
            title:'维护人',
            align:"center",
            dataIndex: 'devLinkman'
          },
          {
            title:'维护人电话',
            align:"center",
            dataIndex: 'devLinkmanTel'
          },
          {
            title:'设备IP',
            align:"center",
            dataIndex: 'devIp'
          },
          {
            title:'设备账号密码',
            align:"center",
            dataIndex: 'devUser'
          },
          {
            title:'启用状态',
            align:"center",
            dataIndex: 'devStatus_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:250,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/iot/devices/list",
          delete: "/iot/devices/delete",
          deleteBatch: "/iot/devices/deleteBatch",
          exportXlsUrl: "/iot/devices/exportXls",
          importExcelUrl: "iot/devices/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      /**
       * 通行记录
       * @param record
       */
      handleRecordList(record){
        this.$refs.recordModal.show(record.devSn);
      },
      /**
       * 心跳信息
       */
      showDevHeart(heartData){
        var da=JSON.parse(heartData);
        let devStatus,rebootcount,runtime,temperature,xeyeruntime;
        for(var i in da){
          if(da[i].metric=="rebootcount"){//重启次数
            rebootcount=da[i].value
          }else if(da[i].metric=="runtime"){//运行长
            runtime=da[i].value
          }else if(da[i].metric=="temperature"){//核心温度
            temperature=da[i].value/1000
          }else if(da[i].metric=="xeyeruntime"){//计算单元
            xeyeruntime=da[i].value
          }
        }
        this.devHeart={
          devStatus:devStatus,
          rebootcount:rebootcount,
          runtime:runtime,
          temperature:temperature,
          xeyeruntime:xeyeruntime
        }
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'devSn',text:'设备编号',dictCode:''})
        fieldList.push({type:'string',value:'devName',text:'设备名称',dictCode:''})
        fieldList.push({type:'int',value:'devType',text:'设备类型',dictCode:'dev_type'})
        fieldList.push({type:'string',value:'devAddress',text:'安装地址',dictCode:''})
        fieldList.push({type:'string',value:'devAddressDetail',text:'详细地址',dictCode:''})
        fieldList.push({type:'string',value:'devLocation',text:'设备坐标',dictCode:''})
        fieldList.push({type:'string',value:'devLinkman',text:'维护人',dictCode:''})
        fieldList.push({type:'string',value:'devLinkmanTel',text:'维护人电话',dictCode:''})
        fieldList.push({type:'string',value:'devIp',text:'设备IP',dictCode:''})
        fieldList.push({type:'string',value:'devUser',text:'设备账号密码',dictCode:''})
        fieldList.push({type:'int',value:'devStatus',text:'状态',dictCode:'ol_status'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>